<template>
  <div class="login-container">
    <div class="login-content">
      <div class="login-title-container">
        <span class="login-title"> 登录 </span>
      </div>
      <el-form
        :model="formObj"
        ref="loginFormRef"
        :rules="loginRules"
        :inline="false"
        size="default"
      >
        <el-form-item prop="username">
          <span class="svg-container">
            <svg-icon icon-class="user"></svg-icon>
          </span>
          <el-input v-model="formObj.username" placeholder="用户名"></el-input>
        </el-form-item>

        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password"></svg-icon>
          </span>
          <el-input
            v-model="formObj.password"
            placeholder="密码"
            :type="passWordType"
            name="password"
          ></el-input>
          <span class="show-pwd" @click="changePwdType">
            <svg-icon
              :icon-class="passWordType === 'password' ? 'eye' : 'eye-open'"
            ></svg-icon>
          </span>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            class="footer"
            :loading="loading"
            @click="handleLogin(loginFormRef)"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
type passWordType = "password" | "text";

import { ref } from "vue";
import { useRouter } from "vue-router";
import type { FormInstance } from "element-plus";
import SvgIcon from "@/components/SvgIcon/index.vue";
import { useUserStore } from "@/store/modules/user";

const formObj = ref({
  username: "admin",
  password: "123456",
});

const passwordValidator = (rule: any, value: any, callback: Function) => {
  if (value.length < 6) {
    callback(new Error("密码长度不能低于6"));
  } else {
    callback();
  }
};

const loginRules = ref({
  username: [
    {
      required: true,
      message: "用户名为必填项",
      trigger: "blur",
    },
  ],
  password: [
    {
      validator: passwordValidator,
      trigger: "blur",
    },
  ],
});

// 密码显示切换
const passWordType = ref<passWordType>("password");

const changePwdType = () => {
  if (passWordType.value === "password") {
    passWordType.value = "text";
  } else {
    passWordType.value = "password";
  }
};

// 登录操作
const loginFormRef = ref<FormInstance>();
const loading = ref<Boolean>(false);
const router = useRouter();
const userStore = useUserStore();
const handleLogin = (formRef: FormInstance | undefined) => {
  console.log("onSubmit");
  if (!formRef) return;
  // 1.表单校验
  formRef.validate((valid) => {
    if (!valid) return;
    loading.value = true;
    // 2.登录
    userStore.login(formObj.value).then((res) => {
      loading.value = false;
      router.push("/");
    });
  });
};
</script>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;
$cursor: #fff;
.login-container {
  min-width: 100%;
  width: 100%;
  height: 100%;
  background: $bg;
  .login-content {
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    .login-title-container {
      color: $light_gray;
      margin-bottom: 35px;
      text-align: center;
      .login-title {
        font-size: 26px;
        font-weight: bold;
      }
      .lang-select {
        float: right;
        background-color: white;
        padding: 4px;
        border-radius: 4px;
        cursor: pointer;
      }
    }

    :deep .el-form-item {
      border: 1px solid rgba(255, 255, 255, 0.1);
      background: rgba(0, 0, 0, 0.1);
      border-radius: 5px;
      color: #454545;
    }
    :deep .el-input {
      display: inline-block;
      height: 47px;
      width: 85%;
      .el-input__wrapper {
        background-color: transparent;
        box-shadow: none;
      }

      input {
        background: transparent;
        border: 0px;
        -webkit-appearance: none;
        border-radius: 0px;
        padding: 12px 5px 12px 15px;
        color: $light_gray;
        height: 47px;
        caret-color: $cursor;
      }
    }

    .svg-container {
      padding: 6px 5px 6px 15px;
      color: $dark_gray;
      vertical-align: middle;
      display: inline-block;
    }

    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select: none;
    }
    .footer {
      width: 100%;
    }
  }
}
</style>
